//
//  mapViewController.m
//  Final
//
//  Created by Pibinuo on 15/12/3.
//  Copyright © 2015年 皮比诺. All rights reserved.
//

#import "mapViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface mapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property(nonatomic,strong)NSMutableArray * placeArray;
@property(nonatomic,strong)CLLocationManager * manager;
@property(nonatomic,strong)CLGeocoder * gecoder;
@property(nonatomic,strong)CLPlacemark *nowPlaceMark;
@end

@implementation mapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.mapView.showsUserLocation = YES;
//    self.placeArray = @[@"通州",@"济南",@"遵义"].mutableCopy;
    [self searchLocation];
}
-(void)searchLocation
{
    [self.manager requestAlwaysAuthorization];
    self.manager.delegate = self;
    [self.manager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *location = locations.firstObject;
    
    [self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        self.nowPlaceMark = placemarks.firstObject;
        
//        NSString * proVience = self.nowPlaceMark.administrativeArea;
//        NSString * city = self.nowPlaceMark.locality;
//        NSString * district = self.nowPlaceMark.subLocality;
//        NSLog(@"%@ %@ %@",proVience,city,district);
        
        
        
        //找出自己的位置
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            //            NSLog(@"%@",self.nowPlaceMark.name);
            NSArray * array = [self.nowPlaceMark.name componentsSeparatedByString:@"区"];
            NSString * str = [array[0] substringFromIndex:2];
            NSString * str1 =[str stringByAppendingString:@"区"];
            NSLog(@"str1 === %@",str1);
//            self.block(str1);
        });
        
    }];
}



-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D corrdinate = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(corrdinate, 2000, 2000);
    CLLocation * loc = (CLLocation *)userLocation;
    [self.mapView setRegion:region animated:YES];
    [self.gecoder reverseGeocodeLocation:loc completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        NSLog(@"%@",loc);
        
    }];
    
    
    
}




// 懒加载
- (CLLocationManager *)manager
{
    if (_manager == nil) {
        _manager = [[CLLocationManager alloc] init];
    }
    return _manager;
}

- (CLGeocoder *)geocoder
{
    if (_gecoder == nil) {
        _gecoder = [[CLGeocoder alloc] init];
    }
    return _gecoder;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
